<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="ID" prop="serviceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.serviceId"-->
<!--          placeholder="请输入ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="服务分类" prop="serviceCategory" >
        <el-select v-model="queryParams.serviceCategory" placeholder="请选择服务分类" clearable style="width: 150px">
          <el-option
              v-for="dict in sys_service_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>

      </el-form-item>

<!--      <el-form-item label="服务分类" prop="serviceCategory" label-width="100px">-->
<!--        <el-select v-model="queryParams.serviceCategory" placeholder="请选择服务分类" clearable>-->
<!--          <el-option-->
<!--              v-for="dict in sys_service_type"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item label="服务编号" prop="serviceCode">
        <el-input
          v-model="queryParams.serviceCode"
          placeholder="请输入服务编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务项目" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务项目"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="userID" prop="merchantId">-->
<!--        <el-input-->
<!--          v-model="queryParams.merchantId"-->
<!--          placeholder="请输入userID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="商家姓名" prop="merchantName">-->
<!--        <el-input-->
<!--          v-model="queryParams.merchantName"-->
<!--          placeholder="请输入商家姓名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="价格" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入价格"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" style="width: 308px">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeCreatedAt"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['masterrabbit-kaixuan:services:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['masterrabbit-kaixuan:services:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['masterrabbit-kaixuan:services:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['masterrabbit-kaixuan:services:export']"
        >导出</el-button>
      </el-col>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="servicesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="serviceId" />-->
      <el-table-column label="服务分类" align="center" prop="serviceCategory">
        <template #default="scope">
          <dict-tag :options="sys_service_type" :value="scope.row.serviceCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="服务编号" align="center" prop="serviceCode" />
      <el-table-column label="服务项目" align="center" prop="serviceName" />
<!--      <el-table-column label="userID" align="center" prop="merchantId" />-->
      <el-table-column label="商家姓名" align="center" prop="merchantName" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="服务介绍" align="center" prop="description" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['masterrabbit-kaixuan:services:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['masterrabbit-kaixuan:services:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务项目对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="servicesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务分类" prop="serviceCategory">
          <el-input v-model="form.serviceCategory" placeholder="请输入服务分类" />
        </el-form-item>
        <el-form-item label="服务编号" prop="serviceCode">
          <el-input v-model="form.serviceCode" placeholder="请输入服务编号" />
        </el-form-item>
        <el-form-item label="服务项目" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务项目" />
        </el-form-item>
        <el-form-item label="userID" prop="merchantId">
          <el-input v-model="form.merchantId" placeholder="请输入userID" />
        </el-form-item>
        <el-form-item label="商家姓名" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商家姓名" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="服务介绍" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Services">
import { listServices, getServices, delServices, addServices, updateServices } from "@/api/masterrabbit-kaixuan/services";

const { proxy } = getCurrentInstance();

const servicesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreatedAt = ref([]);

const { sys_service_type } = proxy.useDict('sys_service_type');

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serviceId: null,
    serviceCategory: null,
    serviceCategoryshow:null,
    serviceCode: null,
    serviceName: null,
    merchantId: null,
    merchantName: null,
    price: null,
    description: null,
    createdAt: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询服务项目列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreatedAt && '' != daterangeCreatedAt) {
    queryParams.value.params["beginCreatedAt"] = daterangeCreatedAt.value[0];
    queryParams.value.params["endCreatedAt"] = daterangeCreatedAt.value[1];
  }
  listServices(queryParams.value).then(response => {
    servicesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    serviceId: null,
    serviceCategory: null,
    serviceCode: null,
    serviceName: null,
    merchantId: null,
    merchantName: null,
    price: null,
    description: null,
    createdAt: null
  };
  proxy.resetForm("servicesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreatedAt.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.serviceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加服务项目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _serviceId = row.serviceId || ids.value
  getServices(_serviceId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改服务项目";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["servicesRef"].validate(valid => {
    if (valid) {
      if (form.value.serviceId != null) {
        updateServices(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addServices(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _serviceIds = row.serviceId || ids.value;
  proxy.$modal.confirm('是否确认删除服务项目编号为"' + _serviceIds + '"的数据项？').then(function() {
    return delServices(_serviceIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('masterrabbit-kaixuan/services/export', {
    ...queryParams.value
  }, `services_${new Date().getTime()}.xlsx`)
}

getList();
</script>
